package com.weixin.project.Controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weixin.project.Entity.ForumQuestion;
import com.weixin.project.Entity.Question;
import com.weixin.project.Service.ForumService;
import com.weixin.project.Service.HttpService;
import com.weixin.project.Service.QuestionService;
import com.weixin.project.Service.SearchService;
import com.weixin.project.Util.CollectionUtil;
import com.weixin.project.Util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
public class SearchController {
    @Value("10")
    Integer searchPageSize;
    @Autowired
    QuestionService questionService;
    @Autowired
    ForumService forumService;
    @Autowired
    SearchService searchService;
    @PostMapping("/search_test")
    JSONObject searchQuestionInBank(@RequestBody JSONObject params) {
        JSONArray jsonArray = params.getJSONArray("tokens");
        Integer page = params.getInteger("page");
        page -- ;
        //没找到更优雅的办法
        String[] tokens = new String[jsonArray.size()];
        for (int i = 0; i < jsonArray.size(); i++) {
            tokens[i] = jsonArray.getString(i);
        }
        List<Question> allQuestions = questionService.searchQuestionsWithTokens(tokens);
        Integer totalPages = allQuestions.size()%10 == 0? allQuestions.size()/searchPageSize :
                allQuestions.size()/searchPageSize + 1;
        List<Question> questions = allQuestions.subList(searchPageSize * page,
                Math.min(allQuestions.size(), searchPageSize * (page + 1)));
        JSONArray array = new JSONArray();
        for (Question question:questions){
            JSONObject obj = new JSONObject();
            obj.put("id", question.getId());
            obj.put("title", question.getStem());
            array.add(obj);
        }
        JSONObject ret = new JSONObject();
        ret.put("pages", totalPages);
        ret.put("results", array);
        return ResponseUtil.success("", ret);
    }

    @PostMapping("/search_question")
    JSONObject searchQuestionInForum(@RequestBody JSONObject params){
        JSONArray jsonArray = params.getJSONArray("tokens");
        Integer page = params.getInteger("page");
        page -- ;
        //没找到更优雅的办法
        String[] tokens = new String[jsonArray.size()];
        for (int i = 0; i < jsonArray.size(); i++) {
            tokens[i] = jsonArray.getString(i);
        }
        List<ForumQuestion> allQuestions = forumService.searchQuestionsWithTokens(tokens);
        Integer totalPages = allQuestions.size()%10 == 0? allQuestions.size()/searchPageSize :
                allQuestions.size()/searchPageSize + 1;
        List<ForumQuestion> questions = allQuestions.subList(searchPageSize * page,
                Math.min(allQuestions.size(), searchPageSize * (page + 1)));
        JSONArray array = new JSONArray();
        for (ForumQuestion question:questions){
            JSONObject obj = new JSONObject();
            obj.put("id", question.getId());
            obj.put("title", question.getTitle());
            array.add(obj);
        }
        JSONObject ret = new JSONObject();
        ret.put("pages", totalPages);
        ret.put("results", array);
        return ResponseUtil.success("", ret);
    }

    @PostMapping("/img/ocr")
    JSONObject imgOCR(@RequestBody JSONObject params){
        JSONObject ret = new JSONObject();
        ret.put("text", searchService.imageOCR(params.getString("image")));
        return ResponseUtil.success(ret);
    }

    @PostMapping("/img/nlp")
    JSONObject imgNLP(@RequestBody JSONObject params){
        JSONObject ret = new JSONObject();
        ret.put("token", searchService.imageNLP(params.getString("text")));
        return ResponseUtil.success(ret);
    }
}
